package com.rdxer.service;

import com.rdxer.core.ex.ListEx;
import com.rdxer.core.utils.IdWorker;
import com.rdxer.model.ClazzStudentRelation;
import com.rdxer.repository.ClazzStudentRelationRepository;
import lombok.Data;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Department 服务层
 */
@Service
@Data
@Getter
public class ClazzStudentRelationService implements CRUDPageServiceInterface<ClazzStudentRelation, String> {


    @Autowired
    private ClazzStudentRelationRepository repository;

    @Autowired
    private IdWorker idWorker;

    public ClazzStudentRelation findByUserId(String userId) {
        List<ClazzStudentRelation> allByUserId = repository.findAllByUserId(userId);
        return ListEx.getFirst(allByUserId);
    }

    public ClazzStudentRelation add(ClazzStudentRelation model) {

        List<ClazzStudentRelation> allByUserIdAndClazzId = repository.findAllByUserIdAndClazzId(model.getUserId(), model.getClazzId());

        if (ListEx.isEmpty(allByUserIdAndClazzId)) {
            setModelDefaultId(model);
            return getRepository().save(model);
        }
        return allByUserIdAndClazzId.get(0);
    }
}
